/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.fileconnector;

import java.util.ResourceBundle;

import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;
import jp.co.withone.osgi.gadget.pictureviewer.PictureController;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/**
 * The Activator class provide initialize and finalize this bundle.
 * 
 * @author funakky
 */
public class Activator implements BundleActivator, SettingViewListener {
	public static final ResourceBundle RB = ResourceBundle
			.getBundle("jp.co.withone.osgi.gadget.fileconnector.resource.message");

	private final Object lock = new Object();
	private boolean stopped = true;
	private ServiceRegistration pictureControllerRegistartion;
	private ServiceRegistration settingViewListenerRegistration;
	private FileConnectorSettingView settingView;

	/**
	 * Regist PictureController service.<br>
	 * This method has called by framework on this bundle start.
	 * 
	 * @param context
	 *            {@link BundleContext} object for this bundle
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 *      )
	 */
	public void start(BundleContext context) throws Exception {
		synchronized (this.lock) {
			this.pictureControllerRegistartion = context.registerService(
					PictureController.class.getName(),
					new PictureControllerImpl(), null);

			this.settingViewListenerRegistration = context.registerService(
					SettingViewListener.class.getName(), this, null);

			this.stopped = false;
		}
	}

	/**
	 * Unregist PictureController service.<br>
	 * This method has called by framework on this bundle stop.
	 * 
	 * @param context
	 *            {@link BundleContext} object for this bundle
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		synchronized (this.lock) {
			this.stopped = true;

			if (this.settingView != null) {
				this.settingView.dispose();
				this.settingView = null;
			}

			this.settingViewListenerRegistration.unregister();
			this.settingViewListenerRegistration = null;

			this.pictureControllerRegistartion.unregister();
			this.pictureControllerRegistartion = null;
		}
	}

	/**
	 * Visible setting view.
	 * 
	 * @param x
	 *            view width position
	 * @param y
	 *            view height position
	 * @see jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener#show()
	 */
	@Override
	public void show(int x, int y) {
		synchronized (this.lock) {
			if (this.stopped) {
				return;
			}
			if (this.settingView == null) {
				this.settingView = new FileConnectorSettingView();
				this.settingView.setBounds(x, y, this.settingView.getWidth(),
						this.settingView.getHeight());
			}
			if (!this.settingView.isVisible()) {
				this.settingView.setVisible(true);
			}
		}
	}

}
